package gui;

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class PanelImagen extends JPanel {

	private Image background;

	public PanelImagen() {
		super();
		this.setOpaque(false);
	}
	
	public PanelImagen(String imgPath){
		super();
		setBackground(imgPath);		
	}
	
	public PanelImagen(Image imagen){
		super();
		setBackground(imagen);
	}

	public void paint(Graphics grafico) {
		
		int width = this.getSize().width;
		int height = this.getSize().height;

		if (this.background != null) {
			try {
			grafico.drawImage(background, 0, 0, width, height, null);
			} catch (Exception e){
				System.err.println("Error en la ruta de la imagen.");
			}
		}

		super.paint(grafico);
	}

	public void setBackground(Image imagen) {
		if (imagen != null) {
			this.setOpaque(false);
			this.background = imagen;
			repaint();
		}
	}
	
	public void setBackground(String imgPath){
		setBackground(new ImageIcon(imgPath).getImage());
	}
}